package com.qqtop10.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Address;
import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.AddressService;
import com.qqtop10.manager.utils.LogHelper;

/**
 * 
 * @ClassName: AddressController
 * @Description: 地区管理Controller
 * @author ChenXinwei
 * @date 2015/9/17
 *
 */
@RequestMapping("/manager/address/")
@Controller
public class AddressController extends BaseController {

	@Autowired
	@Qualifier("addressService")
	private AddressService addressService;

	@RequestMapping("tree.do")
	@ResponseBody
	public Result tree(Address input) {
		try {
			if (input == null) {
				input = new Address();
			}
			if (input.getId() == null) {
				input.setId(100000);/* Root element id. */
			}
			return ok(addressService.tree(input));
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("get.do")
	@ResponseBody
	public Result get(Address input) {
		try {
			return ok(addressService.get(input));
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("delete.do")
	@ResponseBody
	public Result delete(Address input) {
		try {
			if (input == null || input.getId() == null) {
				return err("有些必须的参数没有指定。");
			}
			if (addressService.delete(input)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台地区管理模块","删除成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				return ok("操作成功！");
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("add.do")
	@ResponseBody
	public Result add(Address input) {
		try {
			if (input == null || input.getName() == null || input.getParent_id() == null || input.getPinyin() == null) {
				return err("有些必须的参数没有指定。");
			}
			if (addressService.add(input)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台地区管理模块","添加成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				return ok("操作成功！", input);
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("update.do")
	@ResponseBody
	public Result update(Address input) {
		try {
			if (input == null || input.getId() == null || input.getName() == null || input.getPinyin() == null) {
				return err("有些必须的参数没有指定。");
			}
			if (addressService.update(input)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(),"后台地区管理模块","修改成功",LogHelper.getLocalIp(getRequest()),Tempadmin.getAccount(),"",true);

				return ok("操作成功！");
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("setParent.do")
	@ResponseBody
	public Result setParent(Address input) {
		try {
			if (addressService.setParent(input)) {
				return ok("操作成功！");
			}
			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("buildJs.do")
	@ResponseBody
	public Result buildJs() {
		Address input = new Address();
		StringBuilder sb = new StringBuilder();
		input.setId(100000);
		sb.append('[');
		for (Address item : addressService.tree(input)) {
			appendNode(item, sb);
		}
		sb.setLength(sb.length() - 1);
		sb.append(']');
		System.out.println(sb);
		return ok("生成成功！");
	}

	private void appendNode(Address input, StringBuilder sb) {
		if(input.getLevel_type() == 3) {
			return;
		}
		sb.append('[');
		sb.append(input.getId());
		sb.append(',');
		sb.append('"');
		sb.append(input.getName());
		sb.append('"');
		sb.append(',');
		for (Address item : addressService.tree(input)) {
			appendNode(item, sb);
		}
		sb.setLength(sb.length() - 1);
		sb.append(']');
		sb.append(',');
	}
}
